#codeing:utf-8
import cv2,json
import numpy as np
from math import *
import os,sys
import scipy as sp   ##在numpy基础上实现的部分算法库
import matplotlib.pyplot as plt  ##绘图库
from scipy.optimize import leastsq  ##引入最小二乘法算法

def cv_imread(file_path):
    cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1)
    return cv_img

def demo1(im_path):
    img = cv_imread(im_path)[:,:,0]
    img = cv2.medianBlur(img, 5)
    cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 30,
                               param1=60, param2=60, minRadius=0, maxRadius=0)
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        # draw the outer circle
        cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
        # draw the center of the circle
        cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)
    cv2.imshow('detected circles', cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def demo2(state_id):
    word_1 = [{'id': 2, 'key_no': '100137009001', 'mould_no': 100137009, 'key_name': 'No.A', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 6, 'type_no': '0301', 'x_mh_ratio': 0.719752, 'y_mh_ratio': -0.066184, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.280248, 'key_width_ratio': 0.063599, 'key_height_ratio': 0.022751, 'is_point': None},
             {'id': 3, 'key_no': '100137009002', 'mould_no': 100137009, 'key_name': '业务流水号', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 4, 'type_no': '11', 'x_mh_ratio': 0.045502, 'y_mh_ratio': -0.037229, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.229059, 'key_width_ratio': 0.084798, 'key_height_ratio': 0.018097, 'is_point': None},
             {'id': 4, 'key_no': '100137009003', 'mould_no': 100137009, 'key_name': '医疗机构类型', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 5, 'type_no': '01', 'x_mh_ratio': 0.324199, 'y_mh_ratio': -0.03878, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.263702, 'key_width_ratio': 0.097725, 'key_height_ratio': 0.014995, 'is_point': None},
             {'id': 5, 'key_no': '100137009004', 'mould_no': 100137009, 'key_name': '病历号', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 41, 'type_no': '0301', 'x_mh_ratio': 0.614788, 'y_mh_ratio': -0.040848, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.178904, 'key_width_ratio': 0.052223, 'key_height_ratio': 0.012927, 'is_point': None},
             {'id': 6, 'key_no': '100137009005', 'mould_no': 100137009, 'key_name': '校验码', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 7, 'type_no': '0301', 'x_mh_ratio': 0.821096, 'y_mh_ratio': -0.040848, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.178904, 'key_width_ratio': 0.051706, 'key_height_ratio': 0.017063, 'is_point': None},
             {'id': 7, 'key_no': '100137009006', 'mould_no': 100137009, 'key_name': '住院时间', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 42, 'type_no': '0401', 'x_mh_ratio': 0.036711, 'y_mh_ratio': -0.007756, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.548087, 'key_width_ratio': 0.067218, 'key_height_ratio': 0.016029, 'is_point': None},
             {'id': 8, 'key_no': '100137009007', 'mould_no': 100137009, 'key_name': '住院天数', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 43, 'type_no': '06', 'x_mh_ratio': 0.62151, 'y_mh_ratio': -0.011375, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.17425, 'key_width_ratio': 0.070321, 'key_height_ratio': 0.018097, 'is_point': None},
             {'id': 9, 'key_no': '100137009008', 'mould_no': 100137009, 'key_name': '住院号', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 40, 'type_no': '11', 'x_mh_ratio': 0.822647, 'y_mh_ratio': -0.012927, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.177353, 'key_width_ratio': 0.052223, 'key_height_ratio': 0.014478, 'is_point': None},
             {'id': 10, 'key_no': '100137009009', 'mould_no': 100137009, 'key_name': '姓名', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 1, 'type_no': '0101', 'x_mh_ratio': 0.022234, 'y_mh_ratio': 0.023785, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.203723, 'key_width_ratio': 0.039297, 'key_height_ratio': 0.014478, 'is_point': 1},
             {'id': 11, 'key_no': '100137009010', 'mould_no': 100137009, 'key_name': '性别', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 2, 'type_no': '0102', 'x_mh_ratio': 0.245605, 'y_mh_ratio': 0.022234, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.046019, 'key_width_ratio': 0.036711, 'key_height_ratio': 0.014478, 'is_point': 1},
             {'id': 12, 'key_no': '100137009011', 'mould_no': 100137009, 'key_name': '医保类型', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 14, 'type_no': '01', 'x_mh_ratio': 0.327301, 'y_mh_ratio': 0.020683, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.309204, 'key_width_ratio': 0.069286, 'key_height_ratio': 0.016029, 'is_point': None},
             {'id': 13, 'key_no': '100137009012', 'mould_no': 100137009, 'key_name': '社会保障号码', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 3, 'type_no': '11', 'x_mh_ratio': 0.687694, 'y_mh_ratio': 0.018097, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.312306, 'key_width_ratio': 0.10031, 'key_height_ratio': 0.015512, 'is_point': None},
             {'id': 17, 'key_no': '100137009016', 'mould_no': 100137009, 'key_name': '合计(大写)', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 20, 'type_no': '0104', 'x_mh_ratio': 0.044984, 'y_mh_ratio': 0.319545, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.509307, 'key_width_ratio': 0.075491, 'key_height_ratio': 0.0212, 'is_point': 1},
             {'id': 19, 'key_no': '100137009017', 'mould_no': 100137009, 'key_name': '预缴金额', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 60, 'type_no': '0302', 'x_mh_ratio': 0.039814, 'y_mh_ratio': 0.353154, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.298862, 'key_width_ratio': 0.066701, 'key_height_ratio': 0.015512, 'is_point': None},
             {'id': 20, 'key_no': '100137009018', 'mould_no': 100137009, 'key_name': '补缴金额', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 63, 'type_no': '0302', 'x_mh_ratio': 0.373837, 'y_mh_ratio': 0.349535, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.298345, 'key_width_ratio': 0.068769, 'key_height_ratio': 0.016029, 'is_point': None},
             {'id': 21, 'key_no': '100137009019', 'mould_no': 100137009, 'key_name': '退费金额', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 64, 'type_no': '0302', 'x_mh_ratio': 0.707859, 'y_mh_ratio': 0.347466, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.292141, 'key_width_ratio': 0.069286, 'key_height_ratio': 0.016029, 'is_point': None},
             {'id': 22, 'key_no': '100137009020', 'mould_no': 100137009, 'key_name': '医保统筹支付', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 54, 'type_no': '0302', 'x_mh_ratio': 0.056877, 'y_mh_ratio': 0.38728, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.134436, 'key_width_ratio': 0.099276, 'key_height_ratio': 0.016546, 'is_point': None},
             {'id': 23, 'key_no': '100137009021', 'mould_no': 100137009, 'key_name': '个人账户支付', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 23, 'type_no': '0302', 'x_mh_ratio': 0.241986, 'y_mh_ratio': 0.385729, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.118407, 'key_width_ratio': 0.099793, 'key_height_ratio': 0.014995, 'is_point': None},
             {'id': 24, 'key_no': '100137009022', 'mould_no': 100137009, 'key_name': '其他医保支付', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 24, 'type_no': '0302', 'x_mh_ratio': 0.410548, 'y_mh_ratio': 0.385212, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.175284, 'key_width_ratio': 0.098759, 'key_height_ratio': 0.014478, 'is_point': None},
             {'id': 25, 'key_no': '100137009023', 'mould_no': 100137009, 'key_name': '收款单位(章)', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 8, 'type_no': '0103', 'x_mh_ratio': 0.056877, 'y_mh_ratio': 0.416236, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.308686, 'key_width_ratio': 0.104964, 'key_height_ratio': 0.018614, 'is_point': 1},
             {'id': 26, 'key_no': '100137009024', 'mould_no': 100137009, 'key_name': '收款人(签章)', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 9, 'type_no': '12', 'x_mh_ratio': 0.419855, 'y_mh_ratio': 0.412616, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.249741, 'key_width_ratio': 0.115822, 'key_height_ratio': 0.015512, 'is_point': 1},
             {'id': 27, 'key_no': '100137009025', 'mould_no': 100137009, 'key_name': '开票时间', 'val_direction': 0, 'print_type': 2, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 10, 'type_no': '05', 'x_mh_ratio': 0.827818, 'y_mh_ratio': 0.41365, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.172182, 'key_width_ratio': 0.347983, 'key_height_ratio': 0.027404, 'is_point': None},
             {'id': 59, 'key_no': '100137009015', 'mould_no': 100137009, 'key_name': '个人支付金额', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 23, 'type_no': '0302', 'x_mh_ratio': 0.636505, 'y_mh_ratio': 0.381075, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.363495, 'key_width_ratio': 0.100827, 'key_height_ratio': 0.014995, 'is_point': None},
             {'id': 60, 'key_no': '100137009034', 'mould_no': 100137009, 'key_name': '标题', 'val_direction': 0, 'print_type': 2, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 100, 'type_no': '01', 'x_mh_ratio': 0.499483, 'y_mh_ratio': -0.107549, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.061633, 'key_width_ratio': 0.544984, 'key_height_ratio': 0.046019, 'is_point': None},
             {'id': 61, 'key_no': '100137009035', 'mould_no': 100137009, 'key_name': '合计小写', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 21, 'type_no': '0302', 'x_mh_ratio': 0.808687, 'y_mh_ratio': 0.311789, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.350051, 'key_width_ratio': 0.19774, 'key_height_ratio': 0.031024, 'is_point': None},
             {'id': 14, 'key_no': '100137009013', 'mould_no': 100137009, 'key_name': '收费项目', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 1, 'key_only_no': 15, 'type_no': '01', 'x_mh_ratio': 0.049121, 'y_mh_ratio': 0.057911, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.061531, 'key_height_ratio': 0.014995, 'is_point': None},
             {'id': 15, 'key_no': '100137009014', 'mould_no': 100137009, 'key_name': '金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 1, 'key_only_no': 17, 'type_no': '0302', 'x_mh_ratio': 0.202172, 'y_mh_ratio': 0.058945, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.027404, 'key_height_ratio': 0.014995, 'is_point': None},
             {'id': 16, 'key_no': '100137009015', 'mould_no': 100137009, 'key_name': '个人支付金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 1, 'key_only_no': 19, 'type_no': '0302', 'x_mh_ratio': 0.283351, 'y_mh_ratio': 0.057394, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.093588, 'key_height_ratio': 0.016029, 'is_point': None},
             {'id': 29, 'key_no': '100137009013', 'mould_no': 100137009, 'key_name': '收费项目', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 2, 'key_only_no': 15, 'type_no': '01', 'x_mh_ratio': 0.386246, 'y_mh_ratio': 0.05636, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.058945, 'key_height_ratio': 0.013961, 'is_point': None},
             {'id': 30, 'key_no': '100137009014', 'mould_no': 100137009, 'key_name': '金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 2, 'key_only_no': 17, 'type_no': '0302', 'x_mh_ratio': 0.531541, 'y_mh_ratio': 0.054809, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.027921, 'key_height_ratio': 0.014995, 'is_point': None},
             {'id': 31, 'key_no': '100137009015', 'mould_no': 100137009, 'key_name': '个人支付金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 2, 'key_only_no': 19, 'type_no': '0302', 'x_mh_ratio': 0.61272, 'y_mh_ratio': 0.054292, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.093588, 'key_height_ratio': 0.014995, 'is_point': None},
             {'id': 32, 'key_no': '100137009013', 'mould_no': 100137009, 'key_name': '收费项目', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 3, 'key_only_no': 15, 'type_no': '01', 'x_mh_ratio': 0.720786, 'y_mh_ratio': 0.052223, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.058945, 'key_height_ratio': 0.013961, 'is_point': None},
             {'id': 33, 'key_no': '100137009014', 'mould_no': 100137009, 'key_name': '金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 3, 'key_only_no': 17, 'type_no': '0302', 'x_mh_ratio': 0.867632, 'y_mh_ratio': 0.051706, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.029473, 'key_height_ratio': 0.016546, 'is_point': None},
             {'id': 34, 'key_no': '100137009015', 'mould_no': 100137009, 'key_name': '个人支付金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 3, 'key_only_no': 19, 'type_no': '0302', 'x_mh_ratio': 0.948294, 'y_mh_ratio': 0.051189, 'ratio': 0.4, 'width': 1934, 'height': 775, 'key_distance_ratio': 0.231127, 'key_width_ratio': 0.096174, 'key_height_ratio': 0.014478, 'is_point': None}]

    word_2 = [{'id': 35, 'key_no': '100237001001', 'mould_no': 100237001, 'key_name': 'No.A', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 6, 'type_no': '0301', 'x_mh_ratio': 0.689266, 'y_mh_ratio': -0.04982, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.310734, 'key_width_ratio': 0.052388, 'key_height_ratio': 0.020031, 'is_point': None},
              {'id': 37, 'key_no': '100237001002', 'mould_no': 100237001, 'key_name': '业务流水号', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 4, 'type_no': '11', 'x_mh_ratio': 0.047252, 'y_mh_ratio': -0.020544, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.238315, 'key_width_ratio': 0.086287, 'key_height_ratio': 0.017976, 'is_point': None},
              {'id': 38, 'key_no': '100237001003', 'mould_no': 100237001, 'key_name': '医疗机构类型', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 5, 'type_no': '01', 'x_mh_ratio': 0.336929, 'y_mh_ratio': -0.020031, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.323575, 'key_width_ratio': 0.100668, 'key_height_ratio': 0.01849, 'is_point': None},
              {'id': 39, 'key_no': '100237001004', 'mould_no': 100237001, 'key_name': '校验码', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 7, 'type_no': '0301', 'x_mh_ratio': 0.695429, 'y_mh_ratio': -0.017463, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.304571, 'key_width_ratio': 0.065742, 'key_height_ratio': 0.020544, 'is_point': None},
              {'id': 40, 'key_no': '100237001005', 'mould_no': 100237001, 'key_name': '姓名', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 1, 'type_no': '0101', 'x_mh_ratio': 0.023112, 'y_mh_ratio': 0.019004, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.196199, 'key_width_ratio': 0.038521, 'key_height_ratio': 0.017463, 'is_point': 1},
              {'id': 41, 'key_no': '100237001006', 'mould_no': 100237001, 'key_name': '性别', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 2, 'type_no': '0102', 'x_mh_ratio': 0.237288, 'y_mh_ratio': 0.016436, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.047766, 'key_width_ratio': 0.032357, 'key_height_ratio': 0.014381, 'is_point': 1},
              {'id': 42, 'key_no': '100237001007', 'mould_no': 100237001, 'key_name': '医保类型', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 14, 'type_no': '01', 'x_mh_ratio': 0.32152, 'y_mh_ratio': 0.017976, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.318952, 'key_width_ratio': 0.069337, 'key_height_ratio': 0.019004, 'is_point': None},
              {'id': 43, 'key_no': '100237001008', 'mould_no': 100237001, 'key_name': '社会保障号码', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 3, 'type_no': '11', 'x_mh_ratio': 0.69132, 'y_mh_ratio': 0.017463, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.30868, 'key_width_ratio': 0.09964, 'key_height_ratio': 0.016949, 'is_point': None},
              {'id': 52, 'key_no': '100237001017', 'mould_no': 100237001, 'key_name': '合计(大写)', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 20, 'type_no': '0104', 'x_mh_ratio': 0.043143, 'y_mh_ratio': 0.485362, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.509307, 'key_width_ratio': 0.078582, 'key_height_ratio': 0.019517, 'is_point': 1},
              {'id': 53, 'key_no': '100237001018', 'mould_no': 100237001, 'key_name': '医保统筹支付', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 54, 'type_no': '0302', 'x_mh_ratio': 0.054443, 'y_mh_ratio': 0.519774, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.133025, 'key_width_ratio': 0.101181, 'key_height_ratio': 0.020544, 'is_point': None},
              {'id': 54, 'key_no': '100237001019', 'mould_no': 100237001, 'key_name': '个人账户支付', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 23, 'type_no': '0302', 'x_mh_ratio': 0.238829, 'y_mh_ratio': 0.518233, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.120699, 'key_width_ratio': 0.100154, 'key_height_ratio': 0.020544, 'is_point': None},
              {'id': 55, 'key_no': '100237001020', 'mould_no': 100237001, 'key_name': '其他医保支付', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 24, 'type_no': '0302', 'x_mh_ratio': 0.411402, 'y_mh_ratio': 0.520288, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.175141, 'key_width_ratio': 0.101181, 'key_height_ratio': 0.01849, 'is_point': None},
              {'id': 56, 'key_no': '100237001012', 'mould_no': 100237001, 'key_name': '个人支付金额', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 19, 'type_no': '0302', 'x_mh_ratio': 0.633796, 'y_mh_ratio': 0.522856, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.366204, 'key_width_ratio': 0.091936, 'key_height_ratio': 0.017976, 'is_point': None},
              {'id': 57, 'key_no': '100237001022', 'mould_no': 100237001, 'key_name': '收款单位(章)', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 8, 'type_no': '0103', 'x_mh_ratio': 0.05547, 'y_mh_ratio': 0.546482, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.329892, 'key_width_ratio': 0.10529, 'key_height_ratio': 0.016949, 'is_point': 1},
              {'id': 58, 'key_no': '100237001023', 'mould_no': 100237001, 'key_name': '收款人(签章)', 'val_direction': 4, 'print_type': 0, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 9, 'type_no': '12', 'x_mh_ratio': 0.406266, 'y_mh_ratio': 0.548023, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.329892, 'key_width_ratio': 0.094504, 'key_height_ratio': 0.017463, 'is_point': 1},
              {'id': 62, 'key_no': '100237001024', 'mould_no': 100237001, 'key_name': '标题', 'val_direction': 0, 'print_type': 2, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 100, 'type_no': '01', 'x_mh_ratio': 0.499743, 'y_mh_ratio': -0.096559, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.061633, 'key_width_ratio': 0.540832, 'key_height_ratio': 0.045198, 'is_point': None},
              {'id': 63, 'key_no': '100237001025', 'mould_no': 100237001, 'key_name': '合计小写', 'val_direction': 4, 'print_type': 2, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 21, 'type_no': '0302', 'x_mh_ratio': 0.810991, 'y_mh_ratio': 0.483821, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.180051, 'key_width_ratio': 0.19774, 'key_height_ratio': 0.029789, 'is_point': None},
              {'id': 64, 'key_no': '100237001026', 'mould_no': 100237001, 'key_name': '开票时间', 'val_direction': 0, 'print_type': 2, 'is_table': 0, 'is_repeat': 0, 'table_group': None, 'rows_no': None, 'key_only_no': 10, 'type_no': '05', 'x_mh_ratio': 0.818182, 'y_mh_ratio': 0.549563, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.229583, 'key_width_ratio': 0.372368, 'key_height_ratio': 0.022599, 'is_point': None},
              {'id': 44, 'key_no': '100237001009', 'mould_no': 100237001, 'key_name': '项目/规格', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 1, 'key_only_no': 15, 'type_no': '0204', 'x_mh_ratio': 0.038007, 'y_mh_ratio': 0.052902, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.068824, 'key_height_ratio': 0.017463, 'is_point': None},
              {'id': 45, 'key_no': '100237001010', 'mould_no': 100237001, 'key_name': '数量', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 1, 'key_only_no': 16, 'type_no': '0301', 'x_mh_ratio': 0.301489, 'y_mh_ratio': 0.051875, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.029276, 'key_height_ratio': 0.014381, 'is_point': None},
              {'id': 46, 'key_no': '100237001011', 'mould_no': 100237001, 'key_name': '金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 1, 'key_only_no': 17, 'type_no': '0302', 'x_mh_ratio': 0.360555, 'y_mh_ratio': 0.051361, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.028762, 'key_height_ratio': 0.014895, 'is_point': None},
              {'id': 47, 'key_no': '100237001012', 'mould_no': 100237001, 'key_name': '个人支付金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 1, 'key_only_no': 19, 'type_no': '0302', 'x_mh_ratio': 0.45095, 'y_mh_ratio': 0.051361, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.087314, 'key_height_ratio': 0.016949, 'is_point': None},
              {'id': 48, 'key_no': '100237001009', 'mould_no': 100237001, 'key_name': '项目/规格', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 2, 'key_only_no': 15, 'type_no': '0204', 'x_mh_ratio': 0.541346, 'y_mh_ratio': 0.052388, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.065742, 'key_height_ratio': 0.015922, 'is_point': None},
              {'id': 49, 'key_no': '100237001010', 'mould_no': 100237001, 'key_name': '数量', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 2, 'key_only_no': 16, 'type_no': '0301', 'x_mh_ratio': 0.803287, 'y_mh_ratio': 0.053929, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.027735, 'key_height_ratio': 0.01849, 'is_point': None},
              {'id': 50, 'key_no': '100237001011', 'mould_no': 100237001, 'key_name': '金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 2, 'key_only_no': 17, 'type_no': '0302', 'x_mh_ratio': 0.860298, 'y_mh_ratio': 0.053416, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.029789, 'key_height_ratio': 0.019004, 'is_point': None},
              {'id': 51, 'key_no': '100237001012', 'mould_no': 100237001, 'key_name': '个人支付金额', 'val_direction': 6, 'print_type': 0, 'is_table': 1, 'is_repeat': 1, 'table_group': '项目明细', 'rows_no': 2, 'key_only_no': 19, 'type_no': '0302', 'x_mh_ratio': 0.945557, 'y_mh_ratio': 0.053416, 'ratio': 0.53, 'width': 1947, 'height': 1038, 'key_distance_ratio': 0.412943, 'key_width_ratio': 0.087827, 'key_height_ratio': 0.020031, 'is_point': None}]

    new_word = []
    get_key = ["key_name", "x_mh_ratio", "y_mh_ratio"]
    if state_id == 1:
        word_ = word_1
    else:
        word_ = word_2
    for dic in word_:
        new_dict = {}
        for key in get_key:
            new_dict[key] = dic[key]
        new_word.append(new_dict)
    return new_word

def demo3():
    image_test = np.zeros((500,500))
    a = (200,200)
    b = (200,250)
    k = (b[0]-a[0])/(b[1]-a[1])  #斜率
    print(k)
    leng = sqrt((b[0]-a[0])**2 + (b[1]-a[1])**2)  #直线距离
    angle = degrees(atan(k-1))   #角度
    print(k,angle)
    b_ = int(leng * cos(90 - angle))
    x = a[0]+b_
    y = k*x+leng
    print(b_,leng)
    print(x,y)


    cv2.circle(image_test, a, 3, (255, 0, 0), 4)
    cv2.circle(image_test, b, 3, (255, 0, 0), 4)
    # cv2.circle(image_test, (x,y), 3, (255, 0, 0), 4)
    cv2.imshow("image_test",image_test)
    cv2.waitKey()

def demo4_ImagePrepro(files_dir):
    sys.path.append(os.path.abspath('../servers_9_13'))
    from imageProgress import detection_main2
    dir_list = os.listdir(files_dir)
    for dir in dir_list:
        file_list = os.listdir(os.path.join(files_dir,dir))
        for file in file_list:
            file_path = os.path.join(files_dir,dir,file)
            save_path = os.path.join(files_dir,dir)
            if os.path.isfile(file_path):
                if file[-3:] == "jpg" or file[-3:] == "png":

                    state,image = detection_main2(save_path,file,None)
                    if state:
                        new_file = file.split(".")[0]+"_new.jpg"
                        cv2.imwrite(save_path+"\\"+new_file,image)
                    else:
                        print("--------------无法处理的图像-----------------",os.path.join(save_path,file))

def calculateRotate(point1,point ,degree):

    # nrx = (x - pointx) * cos(angle) - (y - pointy) * sin(angle) + pointx
    # nry = (x - pointx) * sin(angle) + (y - pointy) * cos(angle) + pointy

    if degree<0:
        degree = -degree
        x = (point[0] - point1[0]) * cos(degree) - (point[1] - point1[1]) * sin(degree) + point1[0]
        y = (point[0] - point1[0]) * sin(degree) + (point[1] - point1[1]) * cos(degree) + point1[1]
        x= int(round(x * 100) / 100)
        y= int(round(y * 100) / 100)
        print("逆时针旋转")
    else:
        degree = -degree
        x = (point[0] - point1[0]) * cos(degree) + (point[1] - point1[1]) * sin(degree) + point1[0]
        y = (point[1] - point1[1]) * cos(degree) - (point[0] - point1[0]) * sin(degree) + point1[1]
        x = int(round(x * 100) / 100)
        y = int(round(y * 100) / 100)
        print("顺时针旋转")
    return x,y

def leas_test():
    img = np.zeros((2800,1800))
    data_y = [33, 64, 36, 67, 77, 79, 80, 81, 78, 78, 46, 44, 61,38, 38,38, 38, 38, 38, 38, 30, 36, 88, 30, 60, 31, 189, 397, 529, 665, 694, 771, 923, 1031, 1131, 1224, 1261, 1242, 1241, 1270, 1275, 1272, 1242, 1238, 1291, 1292, 1294, 1303, 1274, 1280, 1290, 1314, 1252, 1233, 1225, 1241, 1059, 845, 663, 603, 644, 481, 367, 280, 326, 308, 502, 536, 675, 733, 849, 994, 1040, 1174, 1253, 1210, 1285, 1279, 1384, 1387, 1388, 1390, 1396, 1362, 1361, 1352, 1384, 1398, 1398, 1405, 1386, 1377, 1382, 1348, 1134, 945, 746, 637, 581, 517, 454, 232, 183, 63, 273, 382, 473, 509, 680, 704, 864, 994, 1054, 1125, 1130, 1207, 1240, 1325, 1373, 1373, 1393, 1374, 1369, 1379, 1383, 1392, 1381, 1372, 1370, 1372, 1361, 1366, 1366, 1334, 1046, 828, 777, 611, 497, 466, 420, 168, 159, 122, 262, 472, 501, 542, 693, 781, 920, 1053, 1126, 1192, 1179, 1229, 1252, 1397, 1412, 1413, 1425, 1424, 1422, 1391, 1420, 1413, 1414, 1413, 1421, 1420, 1395, 1397, 1362, 1127, 922, 852, 700, 568, 556, 554, 399, 219, 124, 230, 370, 472, 539, 704, 734, 953, 969, 1115, 1223, 1307, 1269, 1287, 1358, 1427, 1431, 1439, 1442, 1431, 1399, 1436, 1441, 1430, 1429, 1431, 1438, 1446, 1424, 1391, 1260, 1041, 816, 697, 657, 546, 514, 461, 161, 112, 182, 300, 469, 507, 606, 699, 875, 953, 1071, 1154, 1166, 1176, 1229, 1253, 1376, 1395, 1395, 1391, 1388, 1403, 1408, 1409, 1395, 1408, 1401, 1394, 1397, 1390, 1387, 1326, 1127, 928, 790, 662, 590, 515, 472, 307, 150, 64, 231, 444, 504, 660, 759, 784, 944, 1023, 1141, 1166, 1174, 1230, 1224, 1315, 1397, 1403, 1390, 1392, 1393, 1404, 1410, 1414, 1404, 1396, 1393, 1387, 1400, 1427, 1375, 1257, 987, 803, 718, 611, 546, 479, 442, 155, 0, 170, 298, 470, 537, 660, 701, 841, 993, 1055, 1246, 1238, 1190, 1249, 1268, 1401, 1446, 1453, 1447, 1446, 1447, 1457, 1460, 1457, 1462, 1458, 1449, 1449, 1456, 1448, 1434, 1157, 891, 778, 715, 562, 515, 513, 358, 159, 122, 229, 400, 505, 536, 666, 780, 900, 975, 1099, 1167, 1198, 1238, 1249, 1309, 1425, 1412, 1407, 1414, 1411, 1415, 1428, 1431, 1428, 1407, 1410, 1416, 1413, 1404, 1397, 1358, 1082, 946, 800, 643, 557, 516, 470, 281, 150, 62, 304, 473, 501, 538, 711, 741, 855, 1026, 1101, 1127, 1135, 1164, 1198, 1268, 1353, 1366, 1357, 1368, 1348, 1359, 1361, 1362, 1376, 1352, 1346, 1360, 1360, 1351, 1331, 1293, 1048, 884, 742, 615, 528, 464, 491, 287, 186, 123, 258, 371, 470, 568, 678, 706, 863, 982, 1104, 1172, 1223, 1206, 1253, 1320, 1408, 1402, 1403, 1406, 1403, 1357, 1348, 1360, 1398, 1394, 1392, 1401, 1409, 1395, 1396, 1345, 1175, 947, 742, 642, 580, 515, 530, 273, 151, 92, 200, 307, 473, 526, 659, 665, 843, 1112, 1122, 1244, 1270, 1253, 1329, 1333, 1445, 1446, 1452, 1465, 1465, 1470, 1473, 1468, 1453, 1455, 1458, 1466, 1461, 1452, 1431, 1394, 1147, 998, 775, 749, 571, 598, 507, 292, 182, 61, 232, 397, 489, 508, 719, 757, 847, 945, 1113, 1197, 1198, 1235, 1274, 1285, 1406, 1435, 1431, 1441, 1439, 1402, 1403, 1423, 1435, 1430, 1416, 1441, 1428, 1430, 1443, 1397, 1220, 934, 835, 684, 590, 511, 465, 372, 181, 61, 219, 317, 470, 505, 684, 820, 869, 951, 1064, 1135, 1196, 1308, 1375, 1397, 1517, 1511, 1514, 1516, 1511, 1511, 1518, 1514, 1510, 1511, 1513, 1518, 1517, 1511, 1512, 1512, 1285, 1041, 943, 814, 620, 437, 433, 311, 190, 31, 126, 343, 447, 473, 645, 759, 859, 969, 1126, 1215, 1195, 1230, 1318, 1331, 1423, 1430, 1434, 1429, 1428, 1430, 1434, 1436, 1431, 1425, 1423, 1445, 1445, 1432, 1424, 1407, 1286, 1067, 842, 737, 614, 570, 536, 393, 189, 53, 96, 272, 472, 472, 666, 664, 791, 961, 1134, 1203, 1252, 1246, 1287, 1301, 1447, 1452, 1448, 1452, 1462, 1470, 1464, 1476, 1454, 1456, 1457, 1465, 1468, 1455, 1440, 1429, 1211, 1015, 781, 773, 572, 571, 568, 375, 217, 113, 170, 390, 499, 507, 729, 734, 914, 1013, 1107, 1175, 1199, 1237, 1260, 1283, 1406, 1422, 1437, 1420, 1426, 1434, 1440, 1428, 1426, 1425, 1425, 1432, 1442, 1415, 1431, 1375, 1149, 948, 779, 714, 610, 501, 515, 290, 191, 77, 191, 274, 473, 507, 704, 762, 919, 970, 1136, 1224, 1207, 1267, 1288, 1272, 1417, 1431, 1448, 1435, 1428, 1431, 1450, 1427, 1435, 1442, 1416, 1427, 1466, 1440, 1443, 1418, 1170, 965, 803, 658, 554, 538, 516, 202, 116, 0, 184, 343, 502, 539, 692, 702, 908, 981, 1132, 1180, 1195, 1237, 1262, 1384, 1427, 1431, 1430, 1427, 1426, 1451, 1439, 1431, 1425, 1436, 1416, 1429, 1423, 1419, 1419, 1380, 1172, 969, 775, 658, 609, 571, 490, 221, 149, 166, 261, 394, 503, 567, 673, 738, 884, 1069, 1131, 1199, 1203, 1247, 1270, 1379, 1453, 1453, 1439, 1444, 1437, 1409, 1416, 1430, 1424, 1430, 1430, 1433, 1426, 1431, 1403, 1238, 983, 884, 729, 682, 571, 535, 508, 228, 120, 108, 293, 471, 568, 693, 764, 880, 1015, 1104, 1193, 1196, 1231, 1290, 1307, 1392, 1439, 1435, 1445, 1442, 1426, 1394, 1416, 1439, 1426, 1430, 1440, 1443, 1426, 1440, 1395, 1155, 932, 776, 665, 582, 533, 466, 265, 193, 0, 242, 301, 502, 567, 690, 733, 882, 938, 1134, 1186, 1201, 1186, 1213, 1331, 1410, 1418, 1438, 1437, 1435, 1442, 1444, 1439, 1430, 1430, 1421, 1428, 1428, 1421, 1438, 1385, 1116, 934, 775, 742, 589, 539, 511, 256, 116, 123, 284, 416, 473, 573, 676, 830, 949, 1045, 1108, 1136, 1259, 1332, 1381, 1489, 1541, 1511, 1527, 1500, 1483, 1520, 1515, 1527, 1541, 1508, 1508, 1517, 1511, 1504, 1551, 1479, 1215, 1011, 865, 688, 445, 453, 357, 269, 177, 108, 323, 470, 475, 536, 709, 766, 888, 1023, 1168, 1203, 1170, 1230, 1221, 1363, 1404, 1395, 1400, 1399, 1388, 1352, 1369, 1396, 1394, 1379, 1399, 1409, 1403, 1382, 1386, 1242, 947, 826, 685, 593, 549, 512, 360, 247, 61, 185, 262, 471, 505, 687, 731, 824, 902, 1079, 1125, 1139, 1123, 1214, 1234, 1320, 1360, 1355, 1349, 1354, 1358, 1357, 1363, 1364, 1353, 1345, 1350, 1359, 1356, 1370, 1320, 1172, 881, 861, 698, 591, 552, 463, 276, 157, 30, 179, 375, 471, 502, 604, 706, 838, 921, 1125, 1222, 1179, 1194, 1247, 1343, 1382, 1416, 1412, 1420, 1411, 1388, 1381, 1413, 1405, 1420, 1416, 1415, 1417, 1414, 1422, 1422, 1269, 959, 812, 743, 612, 521, 516, 367, 215, 62, 125, 283, 468, 471, 698, 733, 861, 945, 1075, 1177, 1179, 1199, 1238, 1345, 1398, 1408, 1406, 1415, 1415, 1380, 1379, 1411, 1403, 1404, 1409, 1415, 1417, 1423, 1423, 1385, 1181, 932, 834, 736, 602, 520, 516, 306, 156, 120, 337, 413, 501, 473, 640, 703, 831, 965, 1096, 1155, 1218, 1194, 1266, 1342, 1405, 1414, 1407, 1413, 1411, 1377, 1377, 1404, 1405, 1413, 1409, 1423, 1426, 1416, 1412, 1374, 1266, 1003, 791, 748, 605, 543, 562, 334, 119, 30, 193, 328, 502, 505, 692, 722, 794, 953, 1051, 1191, 1198, 1204, 1298, 1328, 1406, 1409, 1412, 1418, 1427, 1431, 1429, 1429, 1446, 1440, 1433, 1431, 1438, 1435, 1438, 1400, 1236, 961, 821, 698, 629, 571, 540, 320, 209, 100, 126, 342, 500, 505, 710, 749, 899, 972, 1025, 1135, 1186, 1362, 1383, 1478, 1522, 1556, 1549, 1555, 1549, 1552, 1550, 1549, 1555, 1552, 1549, 1557, 1565, 1559, 1571, 1532, 1377, 1125, 966, 841, 673, 472, 425, 322, 153, 30, 155, 261, 456, 473, 663, 750, 829, 910, 965, 1079, 1183, 1335, 1426, 1464, 1524, 1557, 1551, 1552, 1549, 1554, 1552, 1551, 1555, 1553, 1550, 1568, 1566, 1577, 1570, 1564, 1429, 1112, 955, 834, 660, 440, 432, 292, 158, 62, 239, 352, 586, 473, 715, 748, 922, 981, 1026, 1107, 1188, 1321, 1392, 1497, 1542, 1550, 1556, 1562, 1542, 1541, 1542, 1542, 1555, 1560, 1560, 1559, 1565, 1561, 1565, 1539, 1382, 1099, 963, 937, 637, 406, 398, 311, 181, 60, 211, 307, 529, 471, 718, 749, 839, 969, 1056, 1108, 1197, 1296, 1302, 1434, 1501, 1487, 1481, 1472, 1481, 1495, 1479, 1460, 1468, 1494, 1496, 1488, 1488, 1484, 1502, 1481, 1245, 1053, 931, 739, 608, 427, 405, 225, 182, 118, 170, 312, 500, 536, 700, 731, 875, 1025, 1177, 1214, 1237, 1212, 1248, 1376, 1432, 1426, 1429, 1425, 1425, 1422, 1412, 1397, 1431, 1432, 1426, 1430, 1448, 1440, 1462, 1373, 1203, 980, 826, 665, 544, 544, 416, 200, 84, 30, 192, 402, 471, 508, 700, 760, 917, 1080, 1138, 1182, 1207, 1229, 1298, 1409, 1419, 1423, 1429, 1433, 1435, 1431, 1433, 1421, 1435, 1427, 1423, 1429, 1433, 1432, 1423, 1320, 1054, 847, 766, 625, 586, 573, 392, 226, 108, 159, 304, 469, 471, 700, 703, 839, 937, 1023, 1172, 1212, 1207, 1256, 1260, 1425, 1419, 1422, 1430, 1433, 1393, 1385, 1427, 1417, 1434, 1425, 1419, 1431, 1430, 1437, 1400, 1177, 948, 826, 739, 610, 559, 489, 193, 160, 120, 138, 308, 471, 503, 711, 840, 923, 1113, 1211, 1261, 1219, 1247, 1294, 1422, 1442, 1436, 1443, 1439, 1442, 1455, 1454, 1446, 1454, 1454, 1439, 1445, 1457, 1454, 1466, 1400, 1149, 912, 756, 626, 570, 531, 374, 228, 83, 0, 252, 383, 469, 538, 689, 735, 996, 1070, 1141, 1229, 1219, 1291, 1343, 1430, 1448, 1443, 1456, 1458, 1450, 1418, 1444, 1434, 1443, 1444, 1446, 1454, 1446, 1452, 1447, 1378, 1079, 823, 826, 763, 602, 588, 361, 191, 30, 121, 383, 524, 532, 605, 724, 779, 939, 1031, 1151, 1177, 1177, 1217, 1268, 1410, 1407, 1415, 1414, 1414, 1416, 1418, 1417, 1413, 1426, 1412, 1409, 1406, 1409, 1402, 1369, 1261, 993, 851, 734, 593, 547, 532, 387, 188, 83, 213, 322, 527, 516, 670, 714, 788, 941, 1014, 1131, 1291, 1197, 1236, 1284, 1391, 1416, 1420, 1418, 1413, 1428, 1421, 1425, 1417, 1423, 1410, 1410, 1422, 1412, 1415, 1399, 1253, 994, 799, 659, 565, 530, 515, 236, 117, 31, 118, 320, 486, 565, 653, 755, 953, 898, 1020, 1065, 1128, 1363, 1397, 1472, 1504, 1522, 1517, 1519, 1515, 1517, 1517, 1502, 1498, 1512, 1513, 1518, 1520, 1516, 1530, 1498, 1371, 1149, 1003, 827, 668, 585, 611, 258, 179, 67, 31]
    data_y1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 0, 64, 36, 67, 77, 79, 80, 81, 78, 78, 46, 44, 61, 38, 38, 38, 38, 38, 38, 38, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 30, 60, 31, 189, 397, 529, 665, 694, 771, 923, 1031, 1131, 1224, 1261, 1242, 1241, 1270, 1275, 1272, 1242, 1238, 1291, 1292, 1294, 1303, 1274, 1280, 1290, 1314, 1252, 1233, 1225, 1241, 1059, 845, 663, 603, 644, 481, 367, 280, 326, 308, 502, 536, 675, 733, 849, 994, 1040, 1174, 1253, 1210, 1285, 1279, 1384, 1387, 1388, 1390, 1396, 1362, 1361, 1352, 1384, 1398, 1398, 1405, 1386, 1377, 1382, 1348, 1134, 945, 746, 637, 581, 517, 454, 232, 183, 63, 273, 382, 473, 509, 680, 704, 864, 994, 1054, 1125, 1130, 1207, 1240, 1325, 1373, 1373, 1393, 1374, 1369, 1379, 1383, 1392, 1381, 1372, 1370, 1372, 1361, 1366, 1366, 1334, 1046, 828, 777, 611, 497, 466, 420, 168, 159, 122, 262, 472, 501, 542, 693, 781, 920, 1053, 1126, 1192, 1179, 1229, 1252, 1397, 1412, 1413, 1425, 1424, 1422, 1391, 1420, 1413, 1414, 1413, 1421, 1420, 1395, 1397, 1362, 1127, 922, 852, 700, 568, 556, 554, 399, 219, 124, 230, 370, 472, 539, 704, 734, 953, 969, 1115, 1223, 1307, 1269, 1287, 1358, 1427, 1431, 1439, 1442, 1431, 1399, 1436, 1441, 1430, 1429, 1431, 1438, 1446, 1424, 1391, 1260, 1041, 816, 697, 657, 546, 514, 461, 161, 112, 182, 300, 469, 507, 606, 699, 875, 953, 1071, 1154, 1166, 1176, 1229, 1253, 1376, 1395, 1395, 1391, 1388, 1403, 1408, 1409, 1395, 1408, 1401, 1394, 1397, 1390, 1387, 1326, 1127, 928, 790, 662, 590, 515, 472, 307, 150, 64, 231, 444, 504, 660, 759, 784, 944, 1023, 1141, 1166, 1174, 1230, 1224, 1315, 1397, 1403, 1390, 1392, 1393, 1404, 1410, 1414, 1404, 1396, 1393, 1387, 1400, 1427, 1375, 1257, 987, 803, 718, 611, 546, 479, 442, 155, 0, 170, 298, 470, 537, 660, 701, 841, 993, 1055, 1246, 1238, 1190, 1249, 1268, 1401, 1446, 1453, 1447, 1446, 1447, 1457, 1460, 1457, 1462, 1458, 1449, 1449, 1456, 1448, 1434, 1157, 891, 778, 715, 562, 515, 513, 358, 159, 122, 229, 400, 505, 536, 666, 780, 900, 975, 1099, 1167, 1198, 1238, 1249, 1309, 1425, 1412, 1407, 1414, 1411, 1415, 1428, 1431, 1428, 1407, 1410, 1416, 1413, 1404, 1397, 1358, 1082, 946, 800, 643, 557, 516, 470, 281, 150, 62, 304, 473, 501, 538, 711, 741, 855, 1026, 1101, 1127, 1135, 1164, 1198, 1268, 1353, 1366, 1357, 1368, 1348, 1359, 1361, 1362, 1376, 1352, 1346, 1360, 1360, 1351, 1331, 1293, 1048, 884, 742, 615, 528, 464, 491, 287, 186, 123, 258, 371, 470, 568, 678, 706, 863, 982, 1104, 1172, 1223, 1206, 1253, 1320, 1408, 1402, 1403, 1406, 1403, 1357, 1348, 1360, 1398, 1394, 1392, 1401, 1409, 1395, 1396, 1345, 1175, 947, 742, 642, 580, 515, 530, 273, 151, 92, 200, 307, 473, 526, 659, 665, 843, 1112, 1122, 1244, 1270, 1253, 1329, 1333, 1445, 1446, 1452, 1465, 1465, 1470, 1473, 1468, 1453, 1455, 1458, 1466, 1461, 1452, 1431, 1394, 1147, 998, 775, 749, 571, 598, 507, 292, 182, 61, 232, 397, 489, 508, 719, 757, 847, 945, 1113, 1197, 1198, 1235, 1274, 1285, 1406, 1435, 1431, 1441, 1439, 1402, 1403, 1423, 1435, 1430, 1416, 1441, 1428, 1430, 1443, 1397, 1220, 934, 835, 684, 590, 511, 465, 372, 181, 61, 219, 317, 470, 505, 684, 820, 869, 951, 1064, 1135, 1196, 1308, 1375, 1397, 1517, 1511, 1514, 1516, 1511, 1511, 1518, 1514, 1510, 1511, 1513, 1518, 1517, 1511, 1512, 1512, 1285, 1041, 943, 814, 620, 437, 433, 311, 190, 31, 126, 343, 447, 473, 645, 759, 859, 969, 1126, 1215, 1195, 1230, 1318, 1331, 1423, 1430, 1434, 1429, 1428, 1430, 1434, 1436, 1431, 1425, 1423, 1445, 1445, 1432, 1424, 1407, 1286, 1067, 842, 737, 614, 570, 536, 393, 189, 53, 96, 272, 472, 472, 666, 664, 791, 961, 1134, 1203, 1252, 1246, 1287, 1301, 1447, 1452, 1448, 1452, 1462, 1470, 1464, 1476, 1454, 1456, 1457, 1465, 1468, 1455, 1440, 1429, 1211, 1015, 781, 773, 572, 571, 568, 375, 217, 113, 170, 390, 499, 507, 729, 734, 914, 1013, 1107, 1175, 1199, 1237, 1260, 1283, 1406, 1422, 1437, 1420, 1426, 1434, 1440, 1428, 1426, 1425, 1425, 1432, 1442, 1415, 1431, 1375, 1149, 948, 779, 714, 610, 501, 515, 290, 191, 77, 191, 274, 473, 507, 704, 762, 919, 970, 1136, 1224, 1207, 1267, 1288, 1272, 1417, 1431, 1448, 1435, 1428, 1431, 1450, 1427, 1435, 1442, 1416, 1427, 1466, 1440, 1443, 1418, 1170, 965, 803, 658, 554, 538, 516, 202, 116, 0, 184, 343, 502, 539, 692, 702, 908, 981, 1132, 1180, 1195, 1237, 1262, 1384, 1427, 1431, 1430, 1427, 1426, 1451, 1439, 1431, 1425, 1436, 1416, 1429, 1423, 1419, 1419, 1380, 1172, 969, 775, 658, 609, 571, 490, 221, 149, 166, 261, 394, 503, 567, 673, 738, 884, 1069, 1131, 1199, 1203, 1247, 1270, 1379, 1453, 1453, 1439, 1444, 1437, 1409, 1416, 1430, 1424, 1430, 1430, 1433, 1426, 1431, 1403, 1238, 983, 884, 729, 682, 571, 535, 508, 228, 120, 108, 293, 471, 568, 693, 764, 880, 1015, 1104, 1193, 1196, 1231, 1290, 1307, 1392, 1439, 1435, 1445, 1442, 1426, 1394, 1416, 1439, 1426, 1430, 1440, 1443, 1426, 1440, 1395, 1155, 932, 776, 665, 582, 533, 466, 265, 193, 0, 242, 301, 502, 567, 690, 733, 882, 938, 1134, 1186, 1201, 1186, 1213, 1331, 1410, 1418, 1438, 1437, 1435, 1442, 1444, 1439, 1430, 1430, 1421, 1428, 1428, 1421, 1438, 1385, 1116, 934, 775, 742, 589, 539, 511, 256, 116, 123, 284, 416, 473, 573, 676, 830, 949, 1045, 1108, 1136, 1259, 1332, 1381, 1489, 1541, 1511, 1527, 1500, 1483, 1520, 1515, 1527, 1541, 1508, 1508, 1517, 1511, 1504, 1551, 1479, 1215, 1011, 865, 688, 445, 453, 357, 269, 177, 108, 323, 470, 475, 536, 709, 766, 888, 1023, 1168, 1203, 1170, 1230, 1221, 1363, 1404, 1395, 1400, 1399, 1388, 1352, 1369, 1396, 1394, 1379, 1399, 1409, 1403, 1382, 1386, 1242, 947, 826, 685, 593, 549, 512, 360, 247, 61, 185, 262, 471, 505, 687, 731, 824, 902, 1079, 1125, 1139, 1123, 1214, 1234, 1320, 1360, 1355, 1349, 1354, 1358, 1357, 1363, 1364, 1353, 1345, 1350, 1359, 1356, 1370, 1320, 1172, 881, 861, 698, 591, 552, 463, 276, 157, 30, 179, 375, 471, 502, 604, 706, 838, 921, 1125, 1222, 1179, 1194, 1247, 1343, 1382, 1416, 1412, 1420, 1411, 1388, 1381, 1413, 1405, 1420, 1416, 1415, 1417, 1414, 1422, 1422, 1269, 959, 812, 743, 612, 521, 516, 367, 215, 62, 125, 283, 468, 471, 698, 733, 861, 945, 1075, 1177, 1179, 1199, 1238, 1345, 1398, 1408, 1406, 1415, 1415, 1380, 1379, 1411, 1403, 1404, 1409, 1415, 1417, 1423, 1423, 1385, 1181, 932, 834, 736, 602, 520, 516, 306, 156, 120, 337, 413, 501, 473, 640, 703, 831, 965, 1096, 1155, 1218, 1194, 1266, 1342, 1405, 1414, 1407, 1413, 1411, 1377, 1377, 1404, 1405, 1413, 1409, 1423, 1426, 1416, 1412, 1374, 1266, 1003, 791, 748, 605, 543, 562, 334, 119, 30, 193, 328, 502, 505, 692, 722, 794, 953, 1051, 1191, 1198, 1204, 1298, 1328, 1406, 1409, 1412, 1418, 1427, 1431, 1429, 1429, 1446, 1440, 1433, 1431, 1438, 1435, 1438, 1400, 1236, 961, 821, 698, 629, 571, 540, 320, 209, 100, 126, 342, 500, 505, 710, 749, 899, 972, 1025, 1135, 1186, 1362, 1383, 1478, 1522, 1556, 1549, 1555, 1549, 1552, 1550, 1549, 1555, 1552, 1549, 1557, 1565, 1559, 1571, 1532, 1377, 1125, 966, 841, 673, 472, 425, 322, 153, 30, 155, 261, 456, 473, 663, 750, 829, 910, 965, 1079, 1183, 1335, 1426, 1464, 1524, 1557, 1551, 1552, 1549, 1554, 1552, 1551, 1555, 1553, 1550, 1568, 1566, 1577, 1570, 1564, 1429, 1112, 955, 834, 660, 440, 432, 292, 158, 62, 239, 352, 586, 473, 715, 748, 922, 981, 1026, 1107, 1188, 1321, 1392, 1497, 1542, 1550, 1556, 1562, 1542, 1541, 1542, 1542, 1555, 1560, 1560, 1559, 1565, 1561, 1565, 1539, 1382, 1099, 963, 937, 637, 406, 398, 311, 181, 60, 211, 307, 529, 471, 718, 749, 839, 969, 1056, 1108, 1197, 1296, 1302, 1434, 1501, 1487, 1481, 1472, 1481, 1495, 1479, 1460, 1468, 1494, 1496, 1488, 1488, 1484, 1502, 1481, 1245, 1053, 931, 739, 608, 427, 405, 225, 182, 118, 170, 312, 500, 536, 700, 731, 875, 1025, 1177, 1214, 1237, 1212, 1248, 1376, 1432, 1426, 1429, 1425, 1425, 1422, 1412, 1397, 1431, 1432, 1426, 1430, 1448, 1440, 1462, 1373, 1203, 980, 826, 665, 544, 544, 416, 200, 84, 30, 192, 402, 471, 508, 700, 760, 917, 1080, 1138, 1182, 1207, 1229, 1298, 1409, 1419, 1423, 1429, 1433, 1435, 1431, 1433, 1421, 1435, 1427, 1423, 1429, 1433, 1432, 1423, 1320, 1054, 847, 766, 625, 586, 573, 392, 226, 108, 159, 304, 469, 471, 700, 703, 839, 937, 1023, 1172, 1212, 1207, 1256, 1260, 1425, 1419, 1422, 1430, 1433, 1393, 1385, 1427, 1417, 1434, 1425, 1419, 1431, 1430, 1437, 1400, 1177, 948, 826, 739, 610, 559, 489, 193, 160, 120, 138, 308, 471, 503, 711, 840, 923, 1113, 1211, 1261, 1219, 1247, 1294, 1422, 1442, 1436, 1443, 1439, 1442, 1455, 1454, 1446, 1454, 1454, 1439, 1445, 1457, 1454, 1466, 1400, 1149, 912, 756, 626, 570, 531, 374, 228, 83, 0, 252, 383, 469, 538, 689, 735, 996, 1070, 1141, 1229, 1219, 1291, 1343, 1430, 1448, 1443, 1456, 1458, 1450, 1418, 1444, 1434, 1443, 1444, 1446, 1454, 1446, 1452, 1447, 1378, 1079, 823, 826, 763, 602, 588, 361, 191, 30, 121, 383, 524, 532, 605, 724, 779, 939, 1031, 1151, 1177, 1177, 1217, 1268, 1410, 1407, 1415, 1414, 1414, 1416, 1418, 1417, 1413, 1426, 1412, 1409, 1406, 1409, 1402, 1369, 1261, 993, 851, 734, 593, 547, 532, 387, 188, 83, 213, 322, 527, 516, 670, 714, 788, 941, 1014, 1131, 1291, 1197, 1236, 1284, 1391, 1416, 1420, 1418, 1413, 1428, 1421, 1425, 1417, 1423, 1410, 1410, 1422, 1412, 1415, 1399, 1253, 994, 799, 659, 565, 530, 515, 236, 117, 31, 118, 320, 486, 565, 653, 755, 953, 898, 1020, 1065, 1128, 1363, 1397, 1472, 1504, 1522, 1517, 1519, 1515, 1517, 1517, 1502, 1498, 1512, 1513, 1518, 1520, 1516, 1530, 1498, 1371, 1149, 1003, 827, 668, 585, 611, 258, 179, 67, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    data_x = [i for i in range(len(data_y))]
    for i in range(len(data_y)):
        img[i,0:data_y[i]] = 255
    cv2.namedWindow("img",cv2.WINDOW_NORMAL)
    cv2.imshow("img",img)
    print(data_x)
    print(data_y)
    print(np.mean(data_y), np.mean(data_y1))
    print(len(data_x),len(data_y))
    plt.figure(figsize=(8, 6))  ##指定图像比例： 8：6
    plt.scatter(data_x, data_y, color="green", label="data", linewidth=1)
    plt.show()

    def func(a, x):
        k, b = a
        return k * x + b
    def dist(a, x, y):
        return func(a, x) - y

    # x = np.array([48.0, 57.0, 50.0, 54.0, 64.0, 61.0, 43.0, 59.0])
    # y = np.array([165.0, 165.0, 157.0, 170.0, 175.0, 165.0, 155.0, 170.0])
    x = np.array(data_x)
    y = np.array(data_y)
    plt.plot(x, y, 'k.')
    param = [0, 0]
    var = leastsq(dist, param, args=(x, y))
    print(var)
    k, b = var[0]
    print(k, b)
    plt.plot(x, k * x + b, 'o-')
    plt.show()


if __name__ == "__main__":
    # table_path1 = "D:\\aaaaaa数据\\总检测集\\OpenCV_obj\\zhiliang\\table\\1101\\4.jpg"
    # path = 'D:\\aaaaaa数据\\总检测集\\OpenCV_obj\\Images\\2.png'
    # demo1(table_path1)

    # dict_ = demo2()
    # print(dict_)

    # demo3()

    #预处理图像
    files_dir = "E:\\Table_Error_Detection\\"
    demo4_ImagePrepro(files_dir)
    # with open("2.txt","r",encoding="utf-8") as file:
    #     files = file.read()
    #     print(files)

    leas_test()